package com.sentiment.servlet;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bson.Document;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sentiment.bean.OperationInfo;
import com.sentiment.database.bean.SysInfo;
import com.sentiment.database.bean.User;
import com.sentiment.database.dbUtil.DbUtil;
import com.sentiment.tools.Format;
import com.sentiment.tools.Paging;
import com.sentiment.webcollector.net.HttpRequest;
import com.sentiment.webcollector.net.HttpResponse;

/**
 * Servlet implementation class SysInfoServlet
 */
@WebServlet("/SysInfoServlet")
public class SysInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	
		String methodName = request.getParameter("method");
		try{
			Method method = getClass().getMethod(methodName, 
					HttpServletRequest.class, HttpServletResponse.class);
			method.invoke(this, request, response);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 得到系统通知
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void GetSysList(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		List<SysInfo> infolist = new ArrayList<>();
		
		//连接数据库获取权限信息
		DbUtil dbutil = new DbUtil();
		dbutil.openConnection(SysInfo.dbName, SysInfo.collection);
		int page  = Integer.parseInt(request.getParameter("pagenum"));
		List<Document> tlist = dbutil.getPage(page, 10);		
		dbutil.closeConnection();			
		SysInfo info = new SysInfo();
		for(int i = 0; i < tlist.size(); i++){
			info = new SysInfo(tlist.get(i));
			infolist.add(info);
		}
		long count = info.getCount();
		String jsonStr = "";
		if(count != 0){
			ObjectMapper mapper = new ObjectMapper();
			String jsonArr = mapper.writeValueAsString(infolist);
			jsonStr = "{\"recordsTotal\":"+ count +",\"SysInfo\":" + jsonArr+"}";
		}else{
			jsonStr = "{\"recordsTotal\":"+ count +",\"SysInfo\":" + "\"" + OperationInfo.NotFoundSysInfo + "\"" +"}";
		}
		
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(jsonStr);
		
		
	}
	
	
	/**
	 * 查询系统通知
	 * 可输入 用户名、操作人名、日期、邮箱、邮件主题进行查询
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void Query(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		String usercode = request.getParameter("usercode");
		String operatorcode = request.getParameter("operatorcode");
		String senddate = request.getParameter("senddate");
		String email = request.getParameter("email");
		String theme = request.getParameter("theme");
		System.out.println(theme);
		int page  = Integer.parseInt(request.getParameter("pagenum"));
				
		List<String> keylist = new ArrayList<>();
		String user_id = "";
		String operator_id = "";
		String info = "";
		if(!usercode.equals("")){
			User user = new User();
			user = user.getUserByUserCode(usercode);
			if(user != null){
				keylist.add("user_id");
				user_id = user.get_id();
				if(!operatorcode.equals("")){
					user = user.getUserByUserCode(operatorcode);
					if(user != null){
						keylist.add("operator_id");
						operator_id = user.get_id();
					}else{
						info = OperationInfo.NotFoundSysInfo;
					}
				}
			}else{
				info = OperationInfo.NotFoundSysInfo;
			}
		}
		if(!senddate.equals("")){
			keylist.add("senddate");
			senddate = Format.ToDBStrDate(senddate);
		}
		if(!email.equals("")){
			keylist.add("email");
		}
		if(!theme.equals("")){
			keylist.add("theme");
		}
		List<SysInfo> sysinfolist = new ArrayList<>();
		long count = 0;
		if(info == "" && keylist.size() != 0){
			
			SysInfo sysinfo = new SysInfo(user_id, email, theme, null, senddate, operator_id);
			String []keys = keylist.toArray(new String [keylist.size()]);
			
			DbUtil db = new DbUtil();
			db.openConnection(SysInfo.dbName, SysInfo.collection);			
			List<Document> tlist = new ArrayList<>();
			tlist = db.queryMany(sysinfo.toDocument(), keys);
			db.closeConnection();
			count = tlist.size();
			
			if(tlist.size() != 0){
				tlist = Paging.paging(tlist, page, 10);
				for (int i = 0; i < tlist.size(); i++) {
					sysinfo = new SysInfo(tlist.get(i));
					sysinfolist.add(sysinfo);
				}
			}else{
				info = OperationInfo.NotFoundSysInfo;
			}
			
		}
		
		String jsonStr = "";
		if(count != 0){
			ObjectMapper mapper = new ObjectMapper();
			String jsonArr = mapper.writeValueAsString(sysinfolist);
			jsonStr = "{\"recordsTotal\":"+ count +",\"SysInfo\":" + jsonArr+"}";
		}else{
			jsonStr = "{\"recordsTotal\":"+ count +",\"Info\":" + "\"" +info + "\"" +"}";
		}
		System.out.println(jsonStr);
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(jsonStr);
		
		
	}
	
	
}
